package com.vhly.ddxdecompiler.dexreader.file;

import com.vhly.ddxdecompiler.dexreader.file.mixeditem.StringIdItem;
import com.vhly.ddxdecompiler.dexreader.file.mixeditem.TypeIdItem;
import com.vhly.ddxdecompiler.dexreader.io.DexInputStream;

import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-10-14
 * Time: 17:20:04
 *
 * @author vhly[FR]
 */
public class TypeIdsSection extends Section {

    Vector<TypeIdItem> items;

    private DexFile dex;


    public TypeIdsSection(DexFile dexFile) {
        items = new Vector<TypeIdItem>();
        dex = dexFile;
    }

    public void read(DexInputStream din) throws IOException {
        int count = size;
        if (count > 0) {
            TypeIdItem item;
            for (int i = 0; i < count; i++) {
                item = new TypeIdItem(dex);
                item.read(din);
                items.addElement(item);
            }
        }
    }

    public void write(DataOutputStream dout) {
    }

    public void readHeaderPart(DexInputStream din) throws IOException {
        size = din.readInt();
        offset = din.readInt();
        dex.offsetCache.put(Integer.toString(offset), this);
    }

    public TypeIdItem indexOf(int index) {
        TypeIdItem ret = null;
        if (index >= 0) {
            ret = items.elementAt(index);
        }
        return ret;
    }

    public void reloc(ByteArrayInputStream bin, int offset) throws IOException {
        bin.reset();
        TypeIdItem item;
        int size = items.size();
        StringIdsSection strings = dex.getStringIds();
        for (int i = 0; i < size; i++) {
            item = items.elementAt(i);
            int index = item.index;
            StringIdItem idItem = strings.indexOf(index);
            item.setStringIdItem(idItem);
        }
    }
}
